package app.modules.account.persistence.dao;

import app.modules.account.util.conversion.UserConvertUtil;
import app.modules.account.dto.web.WebUserDTO;
import app.modules.account.dto.web.WebUserQueryDTO;
import app.modules.account.persistence.entity.UserEO;
import app.modules.account.persistence.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.StringUtil;
import jasmine.framework.database.mybatisplus.dao.BaseEntityDAO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class UserDAO extends BaseEntityDAO<UserMapper, UserEO> {

    /**
     * 查询用户信息
     *
     * @param queryDTO
     * @param page
     * @return
     */
    public List<WebUserDTO> pageWebUserDTOsByCond(WebUserQueryDTO queryDTO, Page page) {
        CheckUtil.notNull(queryDTO, "queryDTO null");
        CheckUtil.notNull(page, "page null");
        LambdaQueryWrapper<UserEO> wrapper = Wrappers.lambdaQuery();

        String userName = queryDTO.getUserName();
        wrapper.like(StringUtil.isNotEmpty(userName), UserEO::getUserName, userName);
        wrapper.orderByAsc(UserEO::getCreatedDate);

        Page<UserEO> pageResult = baseMapper.selectPage(page, wrapper);
        List<UserEO> recordList = pageResult.getRecords();
        List<WebUserDTO> webUserDtoList = UserConvertUtil.userEO2WebUserDTO(recordList);

        return webUserDtoList;
    }

}
